package com.example.day_20.DataBaseUtils;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.day_20.bean.InFoBean;
import com.example.day_20.net.MyApp;

import java.util.ArrayList;
import java.util.List;

public class DbUtils {
    static String tableName = "user";//表名

    static OpenHelper openHelper = new OpenHelper(MyApp.context);// 创建数据库之后  获得他的对象

    //增
    public static void insert(String title,String url){
        SQLiteDatabase writableDatabase = openHelper.getWritableDatabase();//获取数据库写的权限
        ContentValues contentValues = new ContentValues();
        contentValues.put("title",title);
        contentValues.put("url",url);

        //写入数据库中  添加方法   参数是 （表名，排队，内容）
        writableDatabase.insert(tableName,null,contentValues);
        writableDatabase.close();
    }

    //删  这里是根据id删除对应的本条数据
    public static void delete(String id){
        SQLiteDatabase writableDatabase = openHelper.getWritableDatabase();//获取数据库写的权限

        //当id=？时删除对应的数据
        writableDatabase.delete(tableName,"id=?",new String[]{id});
        writableDatabase.close();
    }

    //改 这里是根据  旧名字 修改成为  新的名字
    public static void upData(String oldData,String newData){
        SQLiteDatabase writableDatabase = openHelper.getWritableDatabase();//获取数据库写的权限

        ContentValues contentValues = new ContentValues();
        contentValues.put("title",newData);
        //当name=? 把    旧的 name    修改成新的   name
        //参数是  表名，内容，条件，参数
        writableDatabase.update(tableName,contentValues,"title=?",new String[]{oldData});
        writableDatabase.close();
    }

    //查   这里查找数据库中所有数据
    public static List<InFoBean.DataBean.DatasBean> select(){
        SQLiteDatabase writableDatabase = openHelper.getWritableDatabase();//获取数据库写的权限
        List<InFoBean.DataBean.DatasBean> datasBeans = new ArrayList<>();
        //查表
        Cursor query = writableDatabase.query(tableName, null, null, null, null, null, null);

        StringBuffer buffer = new StringBuffer();
        while(query.moveToNext()){//while  查表 自动移动到下一条数据
            String title = query.getString(query.getColumnIndex("title"));
            String url = query.getString(query.getColumnIndex("url"));
            //查到之后用  Stringbuffer  拼接起来  这里的\n是用来换行的
            InFoBean.DataBean.DatasBean datasBean = new InFoBean.DataBean.DatasBean();
            datasBean.setTitle(title);
            datasBean.setEnvelopePic(url);
            datasBeans.add(datasBean);
        }
        writableDatabase.close();
        return datasBeans;
    }

    public List<InFoBean.DataBean.DatasBean> queryOne(String title) {
        SQLiteDatabase writableDatabase = openHelper.getWritableDatabase();
        Cursor query = writableDatabase.query(tableName, null, "title = ?", new String[]{title}, null, null, null);

        List<InFoBean.DataBean.DatasBean> beans = new ArrayList<>();
        while (query.moveToNext()) {
            String url = query.getString(query.getColumnIndex("url"));

            InFoBean.DataBean.DatasBean datasBean = new InFoBean.DataBean.DatasBean();
            datasBean.setEnvelopePic(url);
            datasBean.setTitle(title);
            beans.add(datasBean);
        }
        writableDatabase.close();
        return beans;
    }
}
